package com.kangyi.admin.controller;

import com.kangyi.admin.service.NursingHomeBookingService;
import com.kangyi.dto.NursingHomeBookingPageQueryDTO;
import com.kangyi.dto.RejectBookingDTO;
import com.kangyi.result.BookingPageResult;
import com.kangyi.result.PageResult;
import com.kangyi.result.Result;
import com.kangyi.vo.NursingHomeBookingDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Blank
 * @version 1.0
 */
@RestController
@RequestMapping("admin/booking")
@Api(tags = "养老院-预约相关接口")
@Slf4j
public class NursingHomeBookingController {

    @Autowired
    private NursingHomeBookingService nursingHomeBookingService;

    @GetMapping("/page")
    @ApiOperation("分页查询预约")
    public Result<BookingPageResult> pageQuery(NursingHomeBookingPageQueryDTO nursingHomeBookingPageQueryDTO){
        log.info("分页查询预约:{}",nursingHomeBookingPageQueryDTO);
        try {
            BookingPageResult bookingPageResult = nursingHomeBookingService.pageQuery(nursingHomeBookingPageQueryDTO);
            return Result.success(bookingPageResult,"查询成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @DeleteMapping("/delete")
    @ApiOperation("批量删除预约参观审核数据")
    @CacheEvict(cacheNames = "NursingHomeBooking_", allEntries = true)
    public Result deleteBooking(@RequestParam List<Integer> ids){
        log.info("批量删除预约参观审核数据:{}",ids);
        try {
            nursingHomeBookingService.deleteBooking(ids);
            return Result.success("删除成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/agree")
    @ApiOperation("批量同意预约")
    @CacheEvict(cacheNames = "NursingHomeBooking_", allEntries = true)
    public Result agreeBooking(@RequestParam List<Integer> ids){
        log.info("同意预约：{}",ids);
        try {
            nursingHomeBookingService.agreeBooking(ids);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.success("同意成功！");
    }

    @PostMapping("/reject")
    @ApiOperation("拒绝预约")
    @CacheEvict(cacheNames = "NursingHomeBooking_", allEntries = true)
    public Result rejectBooking(RejectBookingDTO rejectBookingDTO){
        log.info("拒绝预约：{}",rejectBookingDTO);
        try {
            nursingHomeBookingService.rejectBooking(rejectBookingDTO);
            return Result.success("拒绝成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/detail/{id}")
    @ApiOperation("查询预约详情")
//    @Cacheable(cacheNames = "NursingHomeBooking_", key = "'adminDetail_'+#id")
    public Result<NursingHomeBookingDetailVO> BookingDetail(@PathVariable Integer id){
        log.info("查询预约详情");
        try {
            NursingHomeBookingDetailVO nursingHomeBookingDetailVO = nursingHomeBookingService.BookingDetail(id);
            return Result.success(nursingHomeBookingDetailVO,"查询成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}
